//
//  NSURLConnection+Blocks.m
//  SteveHolt
//
//  Created by Eric Florenzano on 10/19/11.
//  Copyright (c) 2011 Boilerplate Inc. All rights reserved.
//

#import "NSURLConnection+Blocks.h"

@implementation NSURLConnection (Blocks)

+ (void)asyncRequest:(NSURLRequest *)request 
             success:(void(^)(NSData *, NSURLResponse *))successBlock_ 
             failure:(void(^)(NSData *, NSError *))failureBlock_ {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSURLResponse *response = nil;
        NSError *error = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:request
                                             returningResponse:&response
                                                         error:&error];
        if(error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                failureBlock_(data, error);
            });
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                successBlock_(data, response);
            });
        }
        [pool release];
    });
}

@end